package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njworkorder.DTO.ExcelData;
import com.njworkorder.Entity.Dictionary;
import com.njworkorder.Entity.Section;
import com.njworkorder.Mapper.*;
import com.njworkorder.Utils.FileOperation;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Tag(name = "文件操作",description = "文件读取保存线管接口")
@RestController
@RequestMapping("/fileOperate")
public class FileOperateController {

    private final SectionMapper sectionMapper;
    private final DictionaryMapper dictionaryMapper;
    public FileOperateController(SectionMapper sectionMapper,
                                 DictionaryMapper dictionaryMapper) {
        this.sectionMapper = sectionMapper;
        this.dictionaryMapper = dictionaryMapper;
    }


    @Transactional
    @PostMapping("/uploadExcel")
    public Result<List<ExcelData>> uploadExcel(@RequestParam("excelFile") MultipartFile file) throws IOException, ParseException {

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期时间格式
//        Date date = format.parse("2025-01-21 12:00:00");
//
//        List<ExcelData> dataList = FileOperation.readExcelFile(file);
//
//        LambdaQueryWrapper<Section> sectionQueryWrapper = new LambdaQueryWrapper<Section>()
//                .select(Section::getId,Section::getSectionName,Section::getRouteId)
//                .apply("1=1");
//        List<Section> sections = sectionMapper.selectList(sectionQueryWrapper);
//
//
//        Map<String, List<ExcelData>> collect = dataList.stream().collect(Collectors.groupingBy(ExcelData::getColumn2));
//
//        List<Dictionary> dictionaryList = new ArrayList<>();
//        Dictionary dictionary;
//
//
//        String RouteId = "aa9ec04b-b6c4-47f2-a433-ab9899e96ede";
//
//        for (Map.Entry<String, List<ExcelData>> entry : collect.entrySet()) {
//            List<ExcelData> value = entry.getValue();
//
//            List<Section> list = sections.stream().filter(f -> f.getRouteId().equals(RouteId) && f.getSectionName().contains("扬州西北绕城段")).toList();
//
//            dictionary = new Dictionary();
//            String dictionaryId = UUID.randomUUID().toString();
//            dictionary.setId(dictionaryId);
//            dictionary.setSortField("0");
//            dictionary.setKeysaas("");
//            dictionary.setValusas(value.getFirst().getColumn2());
//            dictionary.setType(value.getFirst().getColumn6().equals("枢纽") ? "interchangeHub" : "serviceArea");
//            dictionary.setSectionId(list.getFirst().getId());
//            dictionary.setRouteId(RouteId);
//            dictionary.setStake(value.getFirst().getColumn3());
//            dictionary.setParentId("");
//            dictionary.setCreateApId("3a85da4e-53d8-4081-9699-bf162c2a4b8b");
//            dictionary.setNotes("");
//            dictionary.setStatus("2");
//            dictionary.setPublishTime(date);
//            dictionaryList.add(dictionary);
//
//            for(ExcelData item : value){
//                dictionary = new Dictionary();
//                dictionary.setId(UUID.randomUUID().toString());
//                dictionary.setSortField("0");
//                dictionary.setKeysaas("");
//                dictionary.setValusas(item.getColumn4());
//                dictionary.setType(value.getFirst().getColumn6().equals("枢纽") ? "interchangeHub" : "serviceArea");
//                dictionary.setSectionId(list.getFirst().getId());
//                dictionary.setRouteId(RouteId);
//                dictionary.setStake(value.getFirst().getColumn3());
//                dictionary.setParentId(dictionaryId);
//                dictionary.setCreateApId("3a85da4e-53d8-4081-9699-bf162c2a4b8b");
//                dictionary.setNotes("");
//                dictionary.setStatus("2");
//                dictionary.setPublishTime(date);
//                dictionaryList.add(dictionary);
//            }
//        }
//        dictionaryMapper.insert(dictionaryList);
//
//
//
//        return ResultUtil.success(dataList);

        return ResultUtil.success("接口暂时停用！");
    }

}
